# PYMT Plugin integration
IS_PYMT_PLUGIN = True
PLUGIN_TITLE = 'Virtual Keyboard Demo'
PLUGIN_AUTHOR = 'Thomas Hansen'
PLUGIN_DESCRIPTION = 'This example shows how to use a virtual keyboard widget which is present int pymt core'


#if __name__ == "__main__":
#    w = MTWindow(fullscreen=False)
#    text_input = MTTextInput()
#    text_input.label="modificando o texto!"
#    text_input.x=100
#    text_input.y=100
#    w.add_widget(text_input)
#    runTouchApp()


import random
from pymt import *
from pyglet.gl import *



class MTSelectableObject(MTTextInput):    
    #construtor
    sizeX = 100
    sizeY = 20
    
    def __init__(self, x,y):
        MTTextInput.__init__(self)
#        super(MTConnectableObject, self).__init__(pos=(x,y), size=(self.sizeX,self.sizeY))
        self.selected = False
        # stores if the user is dragging and with which finger
        self.dragging = (False, -1)
        self.x=x
        self.y=y
        self.oldX=0
        self.oldY=0
        self.width =self.sizeX
        self.height=self.sizeY
        
        
    def on_draw (self):
        if self.selected:
            glColor3d(1,1,1)
            px,py=self.pos
            px-=self.sizeX/2
            py-=self.sizeY/2
            drawRectangle((px, py), size=(self.sizeX*2,self.sizeY*2))
        MTTextInput.on_draw(self)    
            
    def select (self):
        self.selected=True
        
    def unselect (self):
        self.selected=False
    
    #will allow the user to drag the object
    def on_touch_move(self, touches, touchID, x, y): 
        MTTextInput.on_touch_move(self, touches, touchID, x, y)

        isDragging, fingerID = self.dragging
        if isDragging:
            self.oldX=self.x
            self.oldY=self.y
            self.x=x
            self.y=y
            
    
    def on_touch_down(self, touches, touchID, x, y): 
        #unselect the object
        if not self.contains(x, y):
            self.unselect()
            
        if self.selected and self.contains(x, y):
            self.dragging=(True, touchID)
            #print "new (" + str(x) + " "+  str(y)+ ") "
            #print "old " + str(self.pos)
            #self.x=x
            #self.y=y
        
        #testando pra ver se funciona...
        if not self.selected:
            MTTextInput.on_touch_down(self, touches, touchID, x, y)

            
    def on_touch_up(self, touches, touchID, x, y): 
        MTTextInput.on_touch_up(self, touches, touchID, x, y)

        isDragging, fingerID = self.dragging 
        if touchID==fingerID:
            self.dragging=(False, -1)


    #function that returns if this object contains a given point
    def contains (self, x, y):
        if x > self.x and x < (self.x + self.width) and y > self.y and y < (self.y + self.height):
            return True
        else:
            return False
        
    #function that calculates the object's center    
    def calculateCenter(self):
        return (self.x+(self.width/2), self.y+(self.height/2))
    
